package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.handler.UserInfoHandler;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper,BankAccount> implements IBankAccountService{
    @Autowired
    private UserInfoHandler userInfoHandler;
    @Autowired
    private BankAccountMapper bankAccountMapper;

    /**
     * 新增银行账户
     * @param reqDTO
     * @return
     */
    @Override
    public Boolean add(BankAccountUpsertReqDTO reqDTO) {
        CurrentUserInfo userInfo = userInfoHandler.currentUserInfo();
        Long userId = userInfo.getId();

        LambdaQueryWrapper<BankAccount> qw = new LambdaQueryWrapper<>();
        qw.eq(userId!=null,BankAccount::getId,userId);
        Integer i = bankAccountMapper.selectCount(qw);
        if (i>0){
            bankAccountMapper.deleteById(userId);
        }

        BankAccount bankAccount = new BankAccount();
        BeanUtils.copyProperties(reqDTO,bankAccount);
        bankAccount.setCreateTime(LocalDateTime.now());
        bankAccount.setUpdateTime(LocalDateTime.now());
        bankAccount.setId(userId);
        int insert = bankAccountMapper.insert(bankAccount);
        return insert >0 ;

    }

    /**
     * 查询当前用户账户
     * @return
     */
    @Override
    public BankAccount getByUserId() {
        CurrentUserInfo userInfo = userInfoHandler.currentUserInfo();
        Long userId = userInfo.getId();
        LambdaQueryWrapper<BankAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankAccount::getId,userId);
        BankAccount bankAccount = bankAccountMapper.selectOne(queryWrapper);
        return bankAccount;
    }
}
